package com.barks.learning.base.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName HandleQueue.java
 * @Description TODO
 * @createTime 2020年07月11日 09:55:00
 */
@Component
public class TaskQueue {
    private Logger logger = LoggerFactory.getLogger(TaskQueue.class);
    private Map<String,Object> queue = new ConcurrentHashMap<>();

    public void pushTask(String task) {
        new Thread(()->{
            logger.info("任务进入消息队列=>",task);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("任务处理完成 => ",task);
            queue.put(task,"success");
        }).start();
    }

    public String getcompleteTask(){
        Iterator<Map.Entry<String,Object>> iterator =  queue.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Object> next = iterator.next();
            Object obj = next.getValue();
            if(obj != null){
                return next.getKey();
            }
        }
        return null;
    }

    public Object getTaskResult(String task){
        return queue.get(task);
    }

    public void removeTask(String task){
        queue.remove(task);
    }
}
